package com.ruoyi.api.user;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.api.user.requestVo.KnowledgeListRequestVo;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.user.domain.Knowledge;
import com.ruoyi.user.service.IKnowledgeService;

import io.swagger.annotations.ApiOperation;

/**
 * 健康知识 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-18
 */
@Controller
@RequestMapping("/api/knowledge")
public class ApiKnowledgeController extends BaseController
{
	private String prefix = "knowledge";
	
	@Autowired
	private IKnowledgeService knowledgeService;
	
	/**
	 * 查询健康知识列表
	 */
	@PostMapping("/list")
	@ResponseBody
	@ApiOperation(value="查询健康知识列表", notes="查询健康知识列表",produces = "application/json")
	public HashMap<String, Object> list(@RequestBody KnowledgeListRequestVo knowledgeListRequestVo)
	{
        List<Knowledge> list = knowledgeService.selectKnowledgeListByParam(knowledgeListRequestVo.getDataId(),knowledgeListRequestVo.getPageSize());
        for(Knowledge kn:list){
        	kn.setContent(kn.getContent()==null?"":kn.getContent().replaceAll("</?[^<]+>",""));
        }
        HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("knowledgeList", list);
		if(list.size()!=0){
			map.put("lastId", list.get(list.size()-1).getId());
		}else{
			map.put("lastId",-1);
		}
        /*String json = JSONUtil.parseMapToJson(map);*/
		return successApp(map);
	}
	
	/**
	 * 查询健康知识详情
	 */
	/*@PostMapping("/info")
	@ApiOperation(value="查询健康知识详情", notes="查询健康知识详情",produces = "application/json")
	public String info(@RequestBody KnowledgeInfoRequestVo infoRequestVo,ModelMap mmap)
	{
		if(infoRequestVo.getId()!=null&&!"".equals(infoRequestVo.getId())){
			Integer kid = Integer.valueOf(infoRequestVo.getId());
			Knowledge knowledge = knowledgeService.selectKnowledgeById(kid);
			mmap.put("knowledge", knowledge);
		    return prefix + "/knowledgeApp";
			return successApp(knowledge);
		}else{
			return prefix + "/knowledgeApp";
		}
		
	}*/
	
	/**
	 * 查询健康知识详情
	 */
	@GetMapping("/info")
	@ApiOperation(value="查询健康知识详情", notes="查询健康知识详情",produces = "application/json")
	public String info(String id,ModelMap mmap)
	{
		if(id!=null&&!"".equals(id)){
			Integer kid = Integer.valueOf(id);
			Knowledge knowledge = knowledgeService.selectKnowledgeById(kid);
			mmap.put("knowledge", knowledge);
		    return prefix + "/knowledgeApp";
			/*return successApp(knowledge);*/
		}else{
			return prefix + "/knowledgeApp";
		}
		
	}
	
}
